/********************************************************
* Projet Chat System
* 
* Yann MBOUNGOU et Jean-Christophe VENDOME
* ELEVES INGENIEUR - 4e Année - Informatique et Réseaux
* INSA TOULOUSE, Dept. GEI - 2014 - 2015
*********************************************************/
package Controller;
import GUI.*;
import ourMessage.*;
import NI.ChatNI;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class ChatController {
    private final ChatGUI chatGUI;
    private final ChatNI chatNI;
    
    public ChatController(ChatGUI chatGUI, ChatNI chatNI) 
    {
       this.chatGUI = chatGUI;
       this.chatNI = chatNI;
    }
    

    //***************Connect the localUser********************//
    public void performConnect(String login) 
    {
        System.out.println("performConnect");
        this.sendHello(login);
    }

    public void sendHello(String login)
    {
        Hello hello = new Hello(login, false); 
        try 
        {
            System.out.println("sendHello");
            this.chatNI.sendBroadcastMessage(hello); 
        } 
        catch (IOException ex) 
        {
           System.err.println("!!ChatCont: IOException ="+ex);
        }
    }
    

    //***************DisConnect the localUser********************//
    public void performDisConnect() 
    {
        System.out.println("performDisconnect");
        this.sendBye(this.chatGUI.displayLogin());
    }
    
    public void sendBye(String login)
    {
        Bye bye = new Bye(login);
        try 
        {
            System.out.println("sendBye");
            this.chatNI.sendBroadcastMessage(bye);
        } 
        catch (IOException ex) 
        {
            System.err.println("!!ChatCont: IOException ="+ex);
        }
    }
    
     //***************Send Text Message********************//
    public void performSend(String login,String msg) 
    {
        System.out.println("performSend");
        this.sendMsg(login,msg);
    }
    
    public void sendMsg(String login,String msg)
    {
        Text text = new Text(login,msg,false);
        try 
        {
            System.out.println("sendMsg");
            this.chatNI.sendBroadcastMessage(text);
        } 
        catch (IOException ex) 
        {
            System.err.println("!!ChatCont: IOException ="+ex);
        }
    }
    
    //***************Send Text Message private********************//
    public void performSendprivate(String login,String msg,InetAddress addr) throws UnknownHostException 
    {
        System.out.println("performSendprivate");
        this.sendMsgprivate(login,msg,addr);
    }
    
    public void sendMsgprivate(String login,String msg, InetAddress addr)
    {
        Text text = new Text(login,msg,true);
        try 
        {
            System.out.println("sendMsgprivate");
            this.chatNI.sendMessage(text,addr);  
        } 
        catch (IOException ex) 
        {
            System.err.println("!!ChatCont: IOException ="+ex);
        }
    }
    
    
    //***************Send HelloAck********************//   
     public void sendHelloAck(InetAddress addr)
    {
        Hello hello = new Hello(this.chatGUI.displayLogin(), true); //  création du msg helloACK
        try 
        {
            System.out.println("sendhelloAck");
            this.chatNI.sendMessage(hello,addr);            
        } 
        catch (IOException ex) 
        {
            System.err.println("!!ChatCont: IOException ="+ex);
        }
    }
    
    //***************Receive Hello********************//
         public void receiveHello(String login)
    {
        System.out.println("receiveHello");
        InetAddress addr=this.chatNI.getServer().getPacket().getAddress(); 
        this.chatGUI.performList(login,addr);//<=== on met a jour notre user liste
        this.sendHelloAck(addr);//<=== on envoie un msg de type hello ack         
    }
         
    //***************Receive HelloACK********************//      
      public void receiveHelloAck(String login)
    {
        System.out.println("receiveHelloAck "+login);
        InetAddress addr=this.chatNI.getServer().getPacket().getAddress(); 
       this.chatGUI.performList(login,addr);     //<=== on met a jour notre user liste   
    }  
      
    //***************Receive Bye********************//     
      public void receiveBye(String login)
      {
          System.out.println("receiveBye");
          this.chatGUI.performBye(login); //<=== on met a jour notre user liste en supprimant un user
      }
      
      //***************Receive Text********************//     
      public void receiveText(String login,String msg)
      {
          System.out.println("receiveText");
          this.chatGUI.ReceiveMsg(login,msg);
      }
      //***************Receive Text Private********************//     
      public void receiveTextPriv(String login,String msg)
      {
          System.out.println("receiveText Private");          
          this.chatGUI.ReceiveMsgPriv(login,msg);
      }
}
